package Http;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.GZIPInputStream;
import javax.imageio.ImageIO;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpConnectionParams;
import org.apache.commons.httpclient.params.HttpMethodParams;

public class HttpWrapper {
	
	private HttpClient cliente;

	public HttpWrapper(){
		this.cliente = getHttpClient();
		this.lastRefered="";
	}

	private HttpClient getHttpClient()
	{
		HttpClient cliente = new HttpClient();
		cliente.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, (int) (30000));  
		cliente.getParams().setParameter(HttpConnectionParams.CONNECTION_TIMEOUT, (int) (50000));
		cliente.getParams().setParameter(HttpConnectionParams.STALE_CONNECTION_CHECK, true);
		cliente.getParams().setParameter(HttpMethodParams.USER_AGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; es-ES; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3");
		return cliente;
	}
	
	private void addHeaders(HttpMethod m)
	{
		m.addRequestHeader("Accept"	, "*/*");
		m.addRequestHeader("Accept-Language", "es-es,es;q=0.8,en-us;q=0.5,en;q=0.3");
		m.addRequestHeader("Accept-Encoding"	, "gzip");
		m.addRequestHeader("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
		m.addRequestHeader("Keep-Alive"	, "115");
		m.addRequestHeader("Connection"	, "keep-alive");
		m.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		m.addRequestHeader("Cookie","login=huevoquilmes; xfss=4wst156iv82aefu2; aff=614531; lang=spanish; per_page_files=50");
	}
	
	private void addHeaders2(HttpMethod m)
	{
		m.addRequestHeader("Accept"	, "*/*");
		m.addRequestHeader("Accept-Language", "es-es,es;q=0.8,en-us;q=0.5,en;q=0.3");
		m.addRequestHeader("Accept-Encoding"	, "");
		m.addRequestHeader("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
		m.addRequestHeader("Keep-Alive"	, "115");
		m.addRequestHeader("Connection"	, "keep-alive");
		m.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		m.addRequestHeader("Cookie","login=huevoquilmes; xfss=4wst156iv82aefu2; aff=614531; lang=spanish; per_page_files=50");
	}
		
	private String lastRefered;
	
	@SuppressWarnings("deprecation")
	public String httpPostPage(String url,String requestBody)
	{
		PostMethod httpPost = new PostMethod(url);
		addHeaders(httpPost);
		if(lastRefered != "" && lastRefered != null){
			httpPost.addRequestHeader("Referer", lastRefered);
		}
		httpPost.setRequestBody(requestBody);
		String page = null;
		try {
			cliente.executeMethod(httpPost);			
			Header a = httpPost.getRequestHeader("location");
			if (a != null){
				this.lastRefered = a.getValue();
			} else {
				lastRefered = url;
			}
			Header h = httpPost.getResponseHeader("Content-Encoding");
			if (h != null && h.getValue().equals("gzip")){
				page = this.unZip(httpPost.getResponseBodyAsStream());
			} else {
				page = httpPost.getResponseBodyAsString();
			}
		} 
		catch (Exception e) {
		} 
		finally {
			httpPost.releaseConnection();
		}
		if(page == null)
			page = "";
		return page.replaceAll("(\\r|\\n)", "");
	}
	
        public String getLastRefered(){
            return lastRefered;
        }
        
	private String unZip(InputStream in){
		GZIPInputStream gzip;
		String str = new String();
		byte[] b = new byte[4096];
		try {
			gzip = new GZIPInputStream(in);
			int a = gzip.read(b);
			while (a != -1){
				str = str + new String(b,0,a);
				a = gzip.read(b) ;
			}
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		return str;
	}


	public BufferedImage httpPostImage(String url)
	{
		PostMethod httpPost = new PostMethod(url);
		addHeaders2(httpPost);
		BufferedImage bimg = null;
		try {
			cliente.executeMethod(httpPost);
			InputStream in = httpPost.getResponseBodyAsStream();
			bimg = ImageIO.read(in);
		} 
		catch (Exception e) {
		} 
		finally {
			httpPost.releaseConnection();
		}
		return bimg;
	}	
}